// const config = { maxLength: 3, cacheKey: 'xglhc-zm-game' }
import { handleStyleFromDifferentColorNum } from '@/config/markSixConfig.js'
import { $utils } from '@utils'
import { useXglhcStore } from '@/stores/xglhc'
import { useInvestStore } from '@/stores/invest'
import { showToast } from 'vant'
import { submitBetNow } from '@/api'
import { isEmpty } from 'lodash-es'
import { useGameStore } from '@/stores/game'

const mixin = {
  data() {
    return {
      selectNumberList: [],
      currentPlayTypeIndex: 0,
      xglhcStore: useXglhcStore(),
      investStore: useInvestStore(),
      gameStore: useGameStore()
    }
  },
  activated() {
    this.notifyParentInvestList(this.getSelectNumOddsList)
  },
  created() {
    // 初始化空数组
    this.initConfig()
  },
  computed: {
    // 获取 游戏赔率
    getNumOddsList() {
      // let data = this.renderData
      if (isEmpty(this.renderData)) return []

      if (this.hasSonPlay) {
        return this.renderData.lotteryPlayTypeList[this.currentPlayTypeIndex].cachePlayList
      }
      return this.renderData.cachePlayList
    },
    currentList() {
      // let data = this.renderData
      if (isEmpty(this.renderData)) return []

      if (this.hasSonPlay) {
        return this.renderData.lotteryPlayTypeList[this.currentPlayTypeIndex].cachePlayList
      }
      return this.renderData.cachePlayList
    },
    // 获取当前游戏选中的数据
    getSelectNumOddsList() {
      if (this.hasSonPlay) {
        return this.selectNumberList[this.currentPlayTypeIndex]
      } else {
        return this.selectNumberList
      }
    },

    // 有子玩法
    hasSonPlay() {
      if ($utils.isEmpty(this.renderData)) return {}
      return !this.renderData.cachePlayList
    },
  },
  methods: {
    initConfig() {
      this.selectNumberList = []
      // 说明有子玩法
      if (this.hasSonPlay) {
        this.renderData?.lotteryPlayTypeList?.forEach(() => {
          this.selectNumberList.push([])
        })
      }
      this.notifyParentInvestList([])
    },
    // 选择游戏类型
    selectPlayType(index) {
      this.currentPlayTypeIndex = index
      this.xglhcStore.setSonGameTypeIndex(index)
      this.notifyParentInvestList(this.getSelectNumOddsList)
      this.initConfig()
    },

    // 选择赔率
    selectNumber(item, preName) {
      // 之前选中的数据
      let list = this.getSelectNumOddsList
      item.betAmount = this.investStore.getSingleInvestMoney
      item['spliceName'] = `${preName}#${item.playName}`
      // 单选的情况
      // if (config.maxLength === 1) {
      //   list = []
      //   list.push(item)
      //   this.notifyParentInvestList(list)
      //   return
      // }

      // 多选的情况
      let findItem = list.map((item) => item.playCode).indexOf(item.playCode)
      if (findItem === -1) {
        list.push(item)
        this.notifyParentInvestList(list)
      } else {
        list.splice(findItem, 1)
        this.notifyParentInvestList(list)
      }
    },

    // 通知父组件
    notifyParentInvestList(list) {
      this.investStore.setInvestNum(list)
    },

    // 重复投注， 填充最后一期投注号码
    fillLastNumber() {
      const list = this.investStore.lastInvestList //最后一次记录
      if (this.hasSonPlay) {
        this.selectNumberList[this.currentPlayTypeIndex] = list
        // this.$set(this.selectNumberList, this.currentPlayTypeIndex, list)
      } else {
        this.selectNumberList = list
      }
      this.investStore.setInputMoney(list[0].betAmount)
      this.notifyParentInvestList(list)
    },

    // 立即下注, 目前暂时有两种格式，
    async betNow() {
      if (!this.investStore.getInvestBtnText.canBat) {
        this.fillLastNumber()
        return
      }

      if (!$utils.verifyBetAmount(this.getSelectNumOddsList)) return

      let params = {
        lotteryCode: this.renderData.lotteryCode, //彩种类型，如六合彩LHC
        orderDetails: [],
      }
      const capCode = this.gameStore.currentHandicapCode
      if (capCode) params.handicapCode = capCode
      this.getSelectNumOddsList.forEach((numItem) => {
        params.orderDetails.push({
          lotteryCode: this.renderData.lotteryCode, // 彩种类型，如六合彩LHC
          playTypeCode: this.renderData.playTypeCode, // 彩种CODE，
          sonPlayTypeCode: numItem.sonPlayTypeCode, // 选中号码 的item信息
          playCode: numItem.playCode, // 选中号码 的item信息
          betContent: numItem.playCode, // 注单内容，与playCode保持一致
          betAmount: numItem.betAmount, // 单注金额
        })
      })
      await submitBetNow(params)

      this.investStore.setLastInvestList(this.getSelectNumOddsList)
      this.initConfig()
      showToast('下注成功')
    },

    // 根据赔率号码类型 处理不同的样式
    handleStyleFromDifferentPlay(item) {
      const type = isNaN(Number(item.playCode)) === false ? 'num' : 'text'
      let styleStore = handleStyleFromDifferentColorNum(item.playCode, type)
      if (this.getSelectNumOddsList.map((item) => item.playCode).includes(item.playCode)) {
        return styleStore.active
      }
      return styleStore.normal
    },

    // 更新投注数据, 如果item有值，则删除一个， 否则
    updateInvestList(type, data) {
      if (type === 'clear') {
        this.initConfig()
        return
      }
      if (type === 'del') {
        if (this.selectNumberList.length > 1) {
          let index = this.selectNumberList[this.currentPlayTypeIndex].findIndex(
            (item) => item.playCode === data.playCode,
          )
          this.selectNumberList[this.currentPlayTypeIndex].splice(index, 1)
        } else {
          let index = this.selectNumberList.findIndex((item) => item.playCode === data.playCode)
          this.selectNumberList.splice(index, 1)
        }
      }
    },
  },
}
export default mixin
